/* 活动记录 */
var requestManager = require('../../../utils/requestManager.js')
var util = require('../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nextUrl: '',
    activityRecordList:[]
  },

  /////////////// 公有方法 ///////////////
  /** 
   * 将网络请求活动数据转化成UI需要的数据模型数组
   */
  resArrayConverToObjectList: function (resArray, dataArray) {
    for (var i = 0; i < resArray.length; i++) {
      let dic = resArray[i];
      let startTime = util.formatUTCDateByFormatStr(dic["start_time"], 'YYYY-MM-DD HH:mm')
      let endTime = util.formatUTCDateByFormatStr(dic["end_time"], 'YYYY-MM-DD HH:mm')
      var activityData = {
        "id": dic["id"],
        'title': dic["title"],
        'startTime': startTime,
        'endTime': endTime,
        'imageUrl': dic['image_url'],
        'productStatue': dic['product_status'],
        'activity_id': dic['littleApplyId']
      }
      dataArray.push(activityData);
    }
  },

  /////////////// 公有方法 ///////////////

  /////////////// 网络请求 ///////////////
  /**
   * 活动记录列表
   */
  requestActivityRecordList: function () {
    var that = this
    requestManager.getRequest('apply/little/apply/user/product/list', {}, function (res) {
      let status = res.status
      if (status == 0) {
        var activityArray = []
        let array = res.results
        that.data.nextUrl = res.next
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, activityArray)
        // 更新UI
        that.setData({
          activityRecordList: activityArray
        })
      }
    })
  },

  /**
   * 删除活动记录
   * activityRecordId 活动记录id
   * selectIndex 对应选中的下标
   */
  requestDelegateActivity: function (activityRecordId, selectIndex) {
    var that = this
    requestManager.postRequest('apply/little/apply/user/product/delete?id=' + activityRecordId, {}, function (res) {
      let status = res.status
      if (status == 0) {
        // 删除成功
        wx.showToast({
          title: '删除活动记录成功！',
          icon: 'none'
        })
        // 取出本地活动列表
        var activityArray = that.data.activityRecordList
        // 删除选中的活动
        activityArray.splice(selectIndex, 1)
        that.setData({
          activityRecordList: activityArray
        })
      }
    })
  },

  /**
   * 上拉加载请求
   */
  requestNextActivityList: function (nextUrl) {
    var that = this
    console.log('nextUrl' + nextUrl)
    let hasff = 'https://wx.extend.xuemei99.com/'
    let next = nextUrl.substring(hasff.length)

    requestManager.getRequest(next, {}, function (res) {
      let status = res.status
      if (status == 0) {
        let array = res.results
        var activityArray = that.data.activityRecordList
        that.data.nextUrl = res.next
        // 将网络请求活动数据转化成UI需要的数据模型数组
        that.resArrayConverToObjectList(array, activityArray)
        // 更新UI
        that.setData({
          activityRecordList: activityArray
        })
      }
    })
  },
  /////////////// 网络请求 ///////////////

  /////////////// 事件 ///////////////
  /**
   * 点击进入活动详情页
   */
  to_activity_detail: function (e) {
    console.log(e)
    let activity_id = e.currentTarget.dataset.activityid
    wx.navigateTo({
      url: '/pages/showView/signUp/weiActivity?activity_id=' + activity_id,
    })
  },

  delegateTap: function (event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let id = event.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除嘛?',
      success:function(res){
        if(res.confirm){
          that.requestDelegateActivity(id, index)
        }
      }
    })
  },
  /////////////// 事件 ///////////////

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()//隐藏转发按钮
    this.requestActivityRecordList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    this.requestActivityRecordList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉加载')
    let tNextUrl = this.data.nextUrl
    if (tNextUrl&&tNextUrl.length > 0) {
      this.requestNextActivityList(tNextUrl)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})